from app.extensions import db


# 设计用户类模型
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(20), nullable=False)
    username = db.Column(db.String(20), unique=True, nullable=False)
    password = db.Column(db.String(100), nullable=False)
    age = db.Column(db.Integer, default=0)
    # 添加反向引用
    blogs = db.relationship('Blog', backref='user', lazy='dynamic')
    comments = db.relationship('Comment', backref='user', lazy='dynamic')


# 设计博客类模型
class Blog(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.Text, nullable=False)
    content = db.Column(db.Text)
    datetime = db.Column(db.DateTime)
    praise_num = db.Column(db.Integer, default=0)
    # 添加外键关联
    uid = db.Column(db.Integer, db.ForeignKey('user.id'))
    # 添加反向引用
    comments = db.relationship('Comment', backref='blog', lazy='dynamic')


class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    content = db.Column(db.Text)
    # 添加外键关联
    bid = db.Column(db.Integer, db.ForeignKey('blog.id'))
    uid = db.Column(db.Integer, db.ForeignKey('user.id'))
